﻿using System.Collections.Generic;
using UnityEngine;
using YooAsset;

namespace Client.Base
{
    public class FsmCreateDownloader : PatchState
    {
        private List<ResourceDownloaderOperation> downloaderOperations = new();
        public override void OnEnter()
        {
            this.Log("[Patch] FsmCreateDownloader.");
            string[] packages = AppInfo.Packages;
            for (int i = 0; i < packages.Length; i++)
                CreateDownloader(packages[i]);

            List<ResourceDownloaderOperation> mergeDownloaders = new();

            for (int i = 0;i < downloaderOperations.Count; i++)
            {
                if (downloaderOperations[i].TotalDownloadCount == 0)
                {
                    continue;
                }

                mergeDownloaders.Add(downloaderOperations[i]);
            }

            if(mergeDownloaders.Count == 0)  //资源最新，不需要更新
            {
                PatchHandle.Instance.Change<FsmStartGame>();
            }
            else
            {
                PatchHandle.Instance.SetDownloaders(mergeDownloaders);
                PatchHandle.Instance.Change<FsmDownloadPackageFiles>();
            }
        }
        private void CreateDownloader(string packageName)
        {
            var package = YooAssets.GetPackage(packageName);
            var downloader = package.CreateResourceDownloader(10, 3);
            downloaderOperations.Add(downloader);

        }
    }
}

